<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>amis admin</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta
            name="viewport"
            content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <link
            rel="stylesheet"
            title="default"
            href="sdk.css"
    />
    <link
            rel="stylesheet"
            href="helper.css"
    />
    <script src="sdk.js"></script>

    <script src="https://unpkg.com/history@4.10.1
/umd/history.js"></script>
    <style>
        html,
        body,
        .app-wrapper {
            position: relative;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        .amis-scope .cxd-Layout-footer {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            z-index: 0;
        }
        .amis-scope .cxd-Layout-content{padding-bottom:6rem;}
        .cxd-AppBcn{display:none}
        .cxd-Panel-heading{display:none}
        .amis-scope .cxd-Layout-headerBar{display:block}
        .amis-scope .cxd-Layout-brandBar{display:none}
    </style>
</head>
<body>
<div id="root" class="app-wrapper"></div>
<script>

    var app = {
        "type": "app",
        "brandName": "应用名称",

        "footer":{
            "columnNum":3,
            "type": "grid-nav",
            "options": [
                {
                    "icon": "https://internal-amis-res.cdn.bcebos.com/images/icon-1.png",
                    "text": "首页",
                    "link":"/pageA/1"
                },
                {
                    "icon": "https://internal-amis-res.cdn.bcebos.com/images/icon-1.png",
                    "text": "交易"
                },
                {
                    "icon": "https://internal-amis-res.cdn.bcebos.com/images/icon-1.png",
                    "text": "我的"
                }
            ]
        },
        "header": {
            "type": "tpl",
            "tpl": "当前时间是：11    "
        },
        "asideBefore": {
            "type": "tpl",
            "tpl": "当前时间是：11    "
        },
        "asideAfter": {
            "type": "tpl",
            "tpl": "当前时间是：11    "
        },
        "pages": [
            {
                label: "Home",
                url: "/",
                redirect: "/pageA"
            },
            {
                label: "示例",

                children: [
                    {
                        label: "页面A",
                        url: "pageA",
                        schema: {
                            type: "page",
                            title: "页面A",
                            body: "页面A"
                        },
                        children: [
                            {
                                label: "页面A-1",
                                url: "1",
                                schema: {
                                    "type": "form",
                                    "title": "优惠券生成",

                                    "body": [
                                        {
                                            "label": "名称",
                                            "type": "input-text",
                                            "name": "text"
                                        },
                                        {
                                            "type": "select",
                                            "label": "封面选择",
                                            "name": "select",
                                            "options": [
                                                {
                                                    "label": "${token}",
                                                    "value": "${token}"
                                                },
                                                {
                                                    "label": "选项B",
                                                    "value": "B"
                                                }
                                            ]
                                        },
                                        {
                                            "type": "input-image",
                                            "label": "图片上传",
                                            "name": "image",
                                            "imageClassName": "r w-full",
                                            "mode": ""
                                        },
                                        {
                                            "type": "input-range",
                                            "label": "折扣力度",
                                            "name": "range"
                                        },
                                        {
                                            "type": "input-text",
                                            "label": "价值",
                                            "name": "text",
                                            "size": "",
                                            "mode": "",
                                            "description": "价值多少元，交易时候衡量数量用",
                                            "placeholder": "单位元"
                                        },
                                        {
                                            "type": "textarea",
                                            "label": "描述",
                                            "name": "textarea"
                                        },
                                        {
                                            "type": "input-number",
                                            "label": "份数",
                                            "name": "number-text"
                                        },
                                        {
                                            "type": "input-file",
                                            "label": "编号上传",
                                            "name": "file",
                                            "description": "逗号或者换行符隔开"
                                        }
                                    ],
                                    "api": "",
                                    "mode": "horizontal"
                                }
                            },
                            {
                                label: "页面A-2",
                                url: "2",
                                schema: {
                                    type: "page",
                                    title: "页面A-2",
                                    body: "页面A-2"
                                }
                            },
                            {
                                label: "页面A-3",
                                url: "3",
                                schema: {
                                    type: "page",
                                    title: "页面A-3",
                                    body: "页面A-3"
                                }
                            }
                        ]
                    },
                    {
                        label: "页面B",
                        schema: {
                            type: "page",
                            title: "页面B",
                            body: "页面B"
                        }
                    },
                    {
                        label: "页面C",
                        schema: {
                            type: "page",
                            title: "页面C",
                            body: "页面C"
                        }
                    },
                    {
                        label: "表单",
                        url: "/form",
                        icon: "fa fa-plus",
                        schemaApi: "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/service/schema?type=form"
                    },
                    {
                        label: "列表",
                        url: "/crud/list",
                        icon: "fa fa-list",
                        schemaApi: "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/service/schema?type=crud"
                    }
                ]
            },
            {
                label: "我的",
                children: [
                    {
                        label: "首页",
                        schema: {
                            type: "page",
                            title: "用户管理",
                            body: "首页"
                        }
                    },
                    {
                        label: "外部链接",
                        link: "http://baidu.gitee.io/amis"
                    },
                    {
                        label: "部门管理",
                        schemaApi: "https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/service/form?tpl=tpl3"
                    }
                ]
            }
        ]
    };
    (function () {
        let amis = amisRequire('amis/embed');
        const match = amisRequire('path-to-regexp').match;

        // 如果想用 browserHistory 请切换下这处代码, 其他不用变
        // const history = History.createBrowserHistory();
        const history = History.createHashHistory();



        function normalizeLink(to, location = history.location) {
            to = to || '';

            if (to && to[0] === '#') {
                to = location.pathname + location.search + to;
            } else if (to && to[0] === '?') {
                to = location.pathname + to;
            }

            const idx = to.indexOf('?');
            const idx2 = to.indexOf('#');
            let pathname = ~idx
                ? to.substring(0, idx)
                : ~idx2
                    ? to.substring(0, idx2)
                    : to;
            let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
            let hash = ~idx2 ? to.substring(idx2) : location.hash;

            if (!pathname) {
                pathname = location.pathname;
            } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
                let relativeBase = location.pathname;
                const paths = relativeBase.split('/');
                paths.pop();
                let m;
                while ((m = /^\.\.?\//.exec(pathname))) {
                    if (m[0] === '../') {
                        paths.pop();
                    }
                    pathname = pathname.substring(m[0].length);
                }
                pathname = paths.concat(pathname).join('/');
            }

            return pathname + search + hash;
        }

        function isCurrentUrl(to, ctx) {
            if (!to) {
                return false;
            }
            const pathname = history.location.pathname;
            const link = normalizeLink(to, {
                ...location,
                pathname,
                hash: ''
            });

            if (!~link.indexOf('http') && ~link.indexOf(':')) {
                let strict = ctx && ctx.strict;
                return match(link, {
                    decode: decodeURIComponent,
                    strict: typeof strict !== 'undefined' ? strict : true
                })(pathname);
            }

            return decodeURI(pathname) === link;
        }

        let amisInstance = amis.embed(
            '#root',app,
            {
                'data':{
                    'token':'111'
                },
                location: history.location
            },

            {
                // watchRouteChange: fn => {
                //   return history.listen(fn);
                // },
                requestAdaptor:function(api){
                    return {
                        ...api,
                        headers:{
                            'token':'',
                        }
                    };
                },
                responseAdaptor:function(api, response, query, request){
                    return response;
                },
                updateLocation: (location, replace) => {
                    location = normalizeLink(location);
                    if (location === 'goBack') {
                        return history.goBack();
                    } else if (
                        (!/^https?\:\/\//.test(location) &&
                            location ===
                            history.location.pathname + history.location.search) ||
                        location === history.location.href
                    ) {
                        // 目标地址和当前地址一样，不处理，免得重复刷新
                        return;
                    } else if (/^https?\:\/\//.test(location) || !history) {
                        return (window.location.href = location);
                    }

                    history[replace ? 'replace' : 'push'](location);
                },
                jumpTo: (to, action) => {
                    if (to === 'goBack') {
                        return history.goBack();
                    }

                    to = normalizeLink(to);

                    if (isCurrentUrl(to)) {
                        return;
                    }

                    if (action && action.actionType === 'url') {
                        action.blank === false
                            ? (window.location.href = to)
                            : window.open(to, '_blank');
                        return;
                    } else if (action && action.blank) {
                        window.open(to, '_blank');
                        return;
                    }

                    if (/^https?:\/\//.test(to)) {
                        window.location.href = to;
                    } else if (
                        (!/^https?\:\/\//.test(to) &&
                            to === history.pathname + history.location.search) ||
                        to === history.location.href
                    ) {
                        // do nothing
                    } else {
                        history.push(to);
                    }
                },
                isCurrentUrl: isCurrentUrl,
                theme: 'cxd'
            }
        );

        history.listen(state => {
            amisInstance.updateProps({
                location: state.location || state
            });
        });
    })();
</script>
</body>
</html>
